package com.hotacorp.opencloud.authserver.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import com.hotacorp.opencloud.authserver.bean.SysUserDetail;
import com.hotacorp.opencloud.authserver.feign.UserFeignClient;
import com.hotacorp.opencloud.authserver.service.EnhanceUserDetailsService;
import com.hotacorp.opencloud.common.authcommon.SysUserBean;
import com.hotacorp.opencloud.common.commdata.Result;

/**
 * EnhanceUserDetailsService实现类
 * @author lwg
 *
 */
@Component
public class UserDetailsServiceImpl implements EnhanceUserDetailsService {
	@Autowired
	private UserFeignClient userFeignClient;

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		Result<SysUserBean> result = userFeignClient.findByUsername(username);
		if (result.getCode() == 0) {
			return new SysUserDetail(result.getData());
		} else {
			throw new UsernameNotFoundException(result.getMsg());
		}
	}

	@Override
	public UserDetails loadUserByPhone(String phone) throws UsernameNotFoundException {
		Result<SysUserBean> result = userFeignClient.findByMobile(phone);
		if (result.getCode() == 0) {
			return new SysUserDetail(result.getData());
		} else {
			throw new UsernameNotFoundException(result.getMsg());
		}
	}

}
